package com.tjec.project.system.controller;

import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.mdmSynchronous.mapper.BmsProjectLeaderMapper;
import com.tjec.project.mdmSynchronous.service.IBmsProjectLeaderService;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.service.IBmsProjectContractService;
import com.tjec.project.projectWorkbench.service.IBmsProjectService;
import com.tjec.project.workflow.domain.BmsWorkflow;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目冻结/解冻 Controller
 *
 * @author liuxu
 * @date 2020-05-06
 */
@RestController
@RequestMapping("/system/project")
public class BmsProjectManageController extends BaseController {

    @Autowired
    private IBmsProjectService bmsProjectService;

    @Autowired
    private IBmsProjectLeaderService bmsProjectLeaderService;

    @Autowired
    private BmsProjectLeaderMapper bmsProjectLeaderMapper;

    @Autowired
    private IBmsProjectContractService bmsProjectContractService;

    /**
     * 项目冻结/解冻列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsProject bmsProject) {
        startPage();
        List<BmsProject> list = bmsProjectService.selectBmsProjectList(bmsProject);
        return getDataTable(list);
    }

    /**
     * 获取项目冻结/解冻 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:project:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid) {
        return AjaxResult.success(bmsProjectService.selectBmsProjectById(guid));
    }

    /**
     * 编辑项目冻结/解冻
     */
    @PreAuthorize("@ss.hasPermi('system:project:edit')")
    @Log(title = "项目冻结/解冻 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsProject bmsProject) {
        int result = bmsProjectService.updateBmsProject(bmsProject);
        //手动冻结项目，将未完成流程在BPM中终止
        if(ObjectUtils.nullSafeEquals(bmsProject.getFreezeStatus(), "0")&&result>0){
            BmsWorkflow bmsWorkflow = new BmsWorkflow();
            bmsWorkflow.setProjectId(bmsProject.getGuid());
            bmsProjectService.cancelInstance(bmsWorkflow);
        }
        return toAjax(result);
    }

    /**
     * 全局同步
     */
    @ApiOperation("全局同步-同步项目负责人、项目、项目合同所有数据")
    @Log(title = "全局同步-同步项目负责人、项目、项目合同所有数据", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/globalSynchronize")
    public AjaxResult globalSynchronize() {
        long maxLeader =bmsProjectLeaderMapper.selectMaxTime();
        //同步项目负责人
        bmsProjectLeaderService.synchronous(maxLeader);
        //更新项目
        bmsProjectService.updateBmsProjectByThirdParty("", true,true);
        //更新项目合同
        long maxProjectContract =bmsProjectContractService.selectMaxTime();
        bmsProjectContractService.synchronous(null,maxProjectContract);
        return AjaxResult.success();
    }
}
